<?php

namespace backend\modules\ReceiptNote\controllers;

use backend\models\c2\entity\DeliveryNoteOutput;
use backend\models\c2\entity\ReceiptNoteInput;
use common\models\c2\entity\InventoryReceiptNote;
use common\models\c2\entity\Warehouse;
use common\models\c2\entity\WarehouseNote;
use common\models\c2\search\WarehouseNoteSearch;
use common\models\c2\statics\WarehouseNoteState;
use common\models\c2\statics\WarehouseNoteType;
use kzo\base\models\statics\EntityModelStatus;
use kzo\base\models\statics\ResponseDatum;
use Yii;
use common\models\c2\entity\InventoryDeliveryNote;
use common\models\c2\search\InventoryDeliveryNoteSearch;

use kzo\base\components\controllers\backend\ModelController as Controller;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * DefaultController implements the CRUD actions for InventoryDeliveryNote model.
 */
class InputController extends Controller
{
    public $modelClass = 'common\models\c2\entity\InventoryReceiptNote';

    /**
     * Finds the InventoryDeliveryNote model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return InventoryReceiptNote the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = InventoryReceiptNote::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionIndex($id)
    {
        $model = $this->findModel($id);
        $searchModel = new WarehouseNoteSearch();
        $searchModel->type = WarehouseNoteType::TYPE_RECEIPT;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'model' => $model,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionEdit($id = null)
    {
        $model = WarehouseNote::findOne($id);
        if (!$model) {
            throw new NotFoundHttpException('The requested page does not exist.');
        }


        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                Yii::$app->session->setFlash($model->getMessageName(), [Yii::t('app.c2', 'Saved successful.')]);
            } else {
                Yii::$app->session->setFlash($model->getMessageName(), $model->errors);
            }
        }

        $model->loadItems();

        return (Yii::$app->request->isAjax) ? $this->renderAjax('edit', [ 'model' => $model,]) : $this->render('edit', [ 'model' => $model,]);
    }

    public function actionOnekeyInput($id)
    {
        $warehouseNote = new ReceiptNoteInput();
        $model = $this->findModel($id);
        if (!$model) {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
        $warehouseNote->setAttributes([
            'type' => WarehouseNoteType::TYPE_RECEIPT,
            'ref_note_id' => $id,
            'label' => '',
            'warehouse_id' => $model->warehouse_id,
            'receiver_name' => '',
            'memo' => '',
            'state' => WarehouseNoteState::INIT,
            'status' => EntityModelStatus::STATUS_ACTIVE,
            'position' => 0,
        ]);
        if ($warehouseNote->save()) {
            $responseData = ResponseDatum::getSuccessDatum(['message' => Yii::t('app.c2', 'Operation completed successfully!')], $id);
        } else {
            $responseData = ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Error: operation can not finish!')], $id);
        }
        return $this->asJson($responseData);
    }

    public function actionCommit($id = null)
    {
        $model = ReceiptNoteInput::findOne($id);
        if (!$model) {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
        if ($model->commit()) {
            $responseData = ResponseDatum::getSuccessDatum(['message' => Yii::t('app.c2', 'Operation completed successfully!')], $id);
        } else {
            $responseData = ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Error: operation can not finish!')], $id);
        }
        return $this->asJson($responseData);
    }

    public function actionDelete($id)
    {
        if (WarehouseNote::findOne($id)->delete()) {
            $responseData = ResponseDatum::getSuccessDatum(['message' => Yii::t('app.c2', 'Operation completed successfully!')], $id);
        } else {
            $responseData = ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Error: operation can not finish!!')], $id);
        }
        return $this->asJson($responseData);
    }

    public function actionDeleteSubitem($id) {
        if (($model = \common\models\c2\entity\WarehouseNoteItem::findOne($id)) !== null) {
            if ($model->delete()) {
                $responseData = ResponseDatum::getSuccessDatum(['message' => Yii::t('app.c2', 'Operation completed successfully!')], $id);
            } else {
                $responseData = ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Error: operation can not finish!')], $id);
            }
        } else {
            $responseData = ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Error: Model not found!')], $id);
        }
        return $this->asJson($responseData);
    }


}
